package com.paranoid.shiro04.service.impl;

import com.paranoid.shiro04.entity.Perms;
import com.paranoid.shiro04.entity.User;
import com.paranoid.shiro04.mapper.UserMapper;
import com.paranoid.shiro04.service.UserService;
import com.paranoid.shiro04.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        //明文密码进行md5 + salt + hash

        //1、生成随机salt
        String salt = SaltUtils.getSalt(8);
        //2、将随机salt保存到数据库
        user.setSalt(salt);
        //3、明文密码进行md5 + salt + hash
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
        user.setPassword(md5Hash.toHex());
        //保存user对象
        userMapper.save(user);
    }

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public User findRolesByUserName(String username) {
        return userMapper.findRolesByUserName(username);
    }

    @Override
    public List<Perms> findPermsByRoleId(String id) {
        return userMapper.findPermsByRoleId(id);
    }


}
